package com.fatdetection.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatdetection.demo.annotation.AuthAdmin;
import com.fatdetection.demo.annotation.AuthOperator;
import com.fatdetection.demo.bean.LayuiTableRst;
import com.fatdetection.demo.bean.Operator;
import com.fatdetection.demo.service.OperatorService;
import com.fatdetection.demo.util.RstMsgUtils;
import com.fatdetection.demo.util.TokenUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/opt")
@AuthAdmin
@AuthOperator
public class OptController {

    @Autowired
    OperatorService operatorService;
    @Autowired
    TokenUtils tokenUtils;

    @GetMapping("/getOptsByProjId/{projectId}")
    public Object getOptsByProjId(@PathVariable Integer projectId){
        List<Operator> data = operatorService.getOptsByProjId(projectId);
        return data;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页的条目数", required = true),
            @ApiImplicitParam(paramType = "header", name = "authorization", value = "Bearer Token凭证", required = true),
    })
    @GetMapping("/selectPage/{current}/{size}")
    public Object selectPage(@PathVariable Integer current
            , @PathVariable Integer size){
        Page<Operator> page = operatorService.selectAllPage(current, size);
        LayuiTableRst layuiTableRst = new LayuiTableRst();
        layuiTableRst.setCode(0);
        layuiTableRst.setCount((int) page.getTotal());
        layuiTableRst.setMsg("获取成功");
        layuiTableRst.setData(page.getRecords());
        return layuiTableRst;
    }
    //添加操作员账号
    @PostMapping("/add")
    public Object insertOpt(Operator operator){
        int count = operatorService.insertOpt(operator);
        if(count > 0){
            return RstMsgUtils.success("注册成功", "");
        }else if(count == -2){
            return RstMsgUtils.error("用户名已被注册");
        }else {
            return RstMsgUtils.error("注册失败");
        }
    }

    //修改操作人员信息（根据username）
    @PostMapping("/update")
    public Object editOperator(Operator operator){
        int count = operatorService.editOperator(operator);
        if(count > 0){
            return RstMsgUtils.success("修改成功", "");
        }else{
            return RstMsgUtils.error("修改失败");
        }
    }

    //删除操作人员信息
    @PostMapping("/del")
    public Object delectOperator(String username){
        int count = operatorService.delOperator(username);
        if(count > 0){
            return RstMsgUtils.success("删除成功", "");
        }else{
            return RstMsgUtils.error("删除失败");
        }
    }

    @ApiOperation(value = "操作员登录获取token", notes = "该接口负责操作员登录获取token")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "username", value = "用户名", required = true),
        @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @PostMapping("/login")
    public Object optLogin(String username, String password){
        Map<String, Object> map = new HashMap<>();
        Operator operator = operatorService.login(username, password);
        map.put("userInfo", operator);
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("role", String.valueOf(2));
        String token = tokenUtils.geneToken(params);
        map.put("token", token);
        return operator == null ? RstMsgUtils.error("登录失败") : RstMsgUtils.success("登陆成功", map);
    }

    //获取所有操作人员信息
    @GetMapping("/findAll")
    public List<Operator> findAllOperators(){
        List<Operator> list = operatorService.findAll();
        return list;
    }

    //根据项目获取所有操作人员信息
    @GetMapping("/findByName/{projectName}")
    public List<Operator> findAllOperatorsByProj(@PathVariable("projectName") String projectName) {
        List<Operator> list = operatorService.findAllByProj(projectName);
        return list;
    }


    @GetMapping("/findbyusername/{username}")
    public Operator findByUsername(@PathVariable("username") String username){
        Operator operator = new Operator();
        operator = operatorService.getOptByUserame(username);
        return operator;
    }
}
